package com.flower.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flower.annotation.Log;
import com.flower.domain.entity.Category;
import com.flower.service.CategoryService;
import com.flower.service.ProductService;
import com.flower.utils.AjaxResult;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理端对商城的分类管理
 */
@RestController("admin")
@RequestMapping("/admin/category")
@RequiredArgsConstructor
public class CategoryController {
    private final CategoryService categoryService;

    /**
     * 分页查询分类列表
     */
    @GetMapping("/page")
    public AjaxResult getCategoryPage(
            @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "categoryName", required = false) String categoryName) {
        
        Page<Category> page = new Page<>(pageNumber, pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果有分类名称，添加模糊查询条件
        if (StringUtils.hasText(categoryName)) {
            queryWrapper.like(Category::getCategoryName, categoryName);
        }
        
        // 按ID排序
        queryWrapper.orderByAsc(Category::getCategoryId);
        
        Page<Category> resultPage = categoryService.page(page, queryWrapper);
        return AjaxResult.success(resultPage);
    }

    /**
     * 新增商品分类
     * @param category
     * @return
     */
    @Log(name = "新增商品分类")
    @PostMapping("/add")
    public AjaxResult addCategory(@RequestBody Category category) {
        categoryService.save(category);
        return AjaxResult.success("新增成功");
    }

    /**
     * 修改商品分类
     * @param category
     * @return
     */
    @Log(name = "修改商品分类")
    @PutMapping("/edit")
    public AjaxResult editCategory(@RequestBody Category category) {
        categoryService.updateById(category);
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除商品分类
     * @param id
     * @return
     */
    @Log(name = "删除商品分类")
    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteCategory(@PathVariable Integer id) {
        if (categoryService.getById(id).getCategoryName().equals("默认分类")) {
            return AjaxResult.error("默认分类不能删除");
        }

        categoryService.removeById(id);
        return AjaxResult.success("删除成功");
    }
    
    /**
     * 批量删除分类
     */
    @Log(name = "批量删除商品分类")
    @DeleteMapping("/batchDelete")
    public AjaxResult batchDeleteCategory(@RequestBody List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return AjaxResult.error("未选择要删除的分类");
        }
        categoryService.removeByIdsWithProduct(ids);
        return AjaxResult.success("批量删除成功");
    }

    /**
     * 根据id查询商品分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getCategory(@PathVariable Integer id) {
        return AjaxResult.success(categoryService.getById(id));
    }
}
